# sub/counter_sub.py

import rclpy
from rclpy.node import Node
from std_msgs.msg import String

class CounterSubscriber(Node):
    def __init__(self):
        super().__init__('counter_sub')
        self.subscription = self.create_subscription(
            String,
            'counter_info',
            self.listener_callback,
            10)
        self.subscription  # prevent unused variable warning
        self.clear_count = 0

    def listener_callback(self, msg):
        data = msg.data.split(',')
        counter_value = int(data[0].split(': ')[1])
        time_stamp = data[1].split(': ')[1]
        self.get_logger().info(f'Time: {time_stamp}, Counter: {counter_value}')
        
        if counter_value == 0:
            self.clear_count += 1
            self.get_logger().info(f'嘿！我已经被被清空{self.clear_count} 次了!')

def main(args=None):
    rclpy.init(args=args)
    counter_subscriber = CounterSubscriber()
    rclpy.spin(counter_subscriber)
    counter_subscriber.destroy_node()
    rclpy.shutdown()

if __name__ == '__main__':
    main()
